/*
 * createTime： 2020/08/05 09:52
 * author： wei.wang
 * description: 通过扫描方式加载服务
 */

const files = require.context('@/service/', true, /\.js$/);
const ignore = ['./request.js', './index.js'];
const service = {};

files.keys().filter(e => !ignore.includes(e)).map(key => {
    if (key.includes('--ignore')) {
        return false;
    }
    if (key.split('/').length !== 3) {
        Promise.reject('file error' + key + ' view service folders Refer to test folder ');
        return;
    }
    const item = files(key).default;

    let keySplit = key
        .split('/')
        .filter(item => {
            return item.indexOf('.js') !== -1;
        })[0]
        .replace('.js', '');

    let ObjectName = key.split('/')[1];
    try {
        if (service[ObjectName]) {
            service[ObjectName][keySplit] = new item();
        } else {
            service[ObjectName] = {};
            service[ObjectName][keySplit] = new item();
        }
    } catch {
        Promise.reject('file error' + key + ' view service folders Refer to test folder ');
    }
});

export default service;